var util = require("../../utils/util.js");
// pages/goods-recommend/goods-recommend.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        userInfo: '',
        isShowOptions: !1,
        isShowRedPacket: !1,
        timeLeftList: ['0', '0', ':', '0', '0', ':', '0', '0'],
        dataInfo:{},
        friendInfo:{
            openId:'oOPwp47i1yv08eWdWgXbhqxY7taS'
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(options.friendId){
            this.setData({
                friendId:options.friendId ||'oOPwp47i1yv08eWdWgXbhqxY7taS'
            })
        }
        const _this = this
        const userInfo = wx.getStorageSync('userInfo')
        if (!userInfo) {
            _this.selectComponent('#login').openLoginModal()
        } else {
            _this.setData({
                userInfo
            })
        }
        this.getBossInfo();
        this.countTime();
        this.confirmAddCircle();
        this.getSurprisePurchaseList();
    },
    getBossInfo:function(){
        const _this = this;
        util.get(getApp().api.user.info,{openId:_this.data.friendId}).then(
            function (res) {
                console.log(res);
                if (res.code == 200) {
                    console.log(res.data);
                    _this.setData({
                        friendInfo: res.data || [],
                        ['friendInfo.job']:'本合生物采购'
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    getFollow: function () {
        let _this = this;
        util.get(getApp().api.follow.getFollow).then(
            function (res) {
                console.log(res);
                if (res.code == 200) {
                    console.log(res.data);
                    _this.setData({
                        follow:res.data[0].friend ||{}
                    });
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    getUserInfo: function (e) {
        this.setData({
            userInfo: e.detail.userInfo
        })
    },
    confirmAddCircle: function(friendOpenId) {
        let _this = this;
        util.get(getApp().api.follow.confirmFollow,{masterId:this.data.friendInfo.openId}).then(
            function (res) {
                if (res.code == 200) {
                    _this.setData({
                        isInvite: !!res.data //没邀请过值是false
                    });
                }  else{
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    closeListener: () => {
        wx.navigateTo({
            url: "/pages/goods-personal/goods-personal?freindId="+this.data.friendInfo.openId
        })
    },
    loginListener: () => {
        let pages = getCurrentPages()
        pages[pages.length - 1].onLoad()
    },
    closeOptions: function () {
        this.setData({
            isShowOptions: !1
        })
    },
    openOptions: function (e) {
        let info = e.currentTarget.dataset.info;
        this.setData({
            isShowOptions: !0,
            dataInfo:info
        })
    },
    closeOptionsRedPacket: function () {
        this.setData({
            isShowRedPacket: !1
        })
    },
    openOptionsRedPacket: function (e) {
        let info = e.currentTarget.dataset.info;
        this.setData({
            isShowRedPacket: !0,
            dataInfo:info
        })
    },
    countTime() {
        let _this = this;
        //获取当前时间
        var date = new Date();
        var now = date.getTime();
        //设置截止时间
        var str = "2020/07/15 00:00:00";
        var endDate = new Date(str);
        var end = endDate.getTime();

        //时间差
        var leftTime = end - now;
        //定义变量 d,h,m,s保存倒计时的时间
        var d, h, m, s;
        if (leftTime >= 0) {
            d = Math.floor(leftTime / 1000 / 60 / 60 / 24);
            h = Math.floor(leftTime / 1000 / 60 / 60 % 24);
            m = Math.floor(leftTime / 1000 / 60 % 60);
            s = Math.floor(leftTime / 1000 % 60);
        }
        //将0-9的数字前面加上0，例1变为01
        //d = checkTime(d);
        h = checkTime(h);
        m = checkTime(m);
        s = checkTime(s);

        function checkTime(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }

        //将倒计时赋值到div中
        //document.getElementById("_d").innerHTML = d+"天";
        let string = h + ":" + m + ":" + s;
        let arr = string.split("");
        _this.setData({
            timeLeftList: arr
        });
        //递归每秒调用countTime方法，显示动态时间效果
        setTimeout(_this.countTime, 1000);
    },
    getSurprisePurchaseList() {
        let _this = this;
        util.get(getApp().api.follow.getSurprisePurchaseList).then(
            function (res) {
                console.log(res);
                if (res.code == 200) {
                    console.log(res.data);
                    for (var i = 0; i < res.data.length; i++) {
                        res.data[i].icon1 = res.data[i].icon1||[];
                        res.data[i].icon1 = res.data[i].icon1.slice(0,3);
                    }
                    _this.setData({
                        list:res.data ||[]
                    });
                    if(res.data.length!=0){
                        _this.setData({
                            dataInfo:res.data[0]
                        });
                    }
                } else {
                    wx.showToast({
                        title: res.msg, //提示的内容,
                        icon: "none", //图标,
                        //duration: 1500, //延迟时间,
                        mask: false, //显示透明蒙层，防止触摸穿透,
                    });
                    setTimeout(function () {
                        wx.hideToast();
                    }, 1500);
                }
            }
        );
    },
    toOrder:function(){
        let _this = this;
        const isLogin = wx.getStorageSync('openid');
        if (isLogin) {
            wx.setStorageSync("orderItem", JSON.stringify({
                productId: _this.data.dataInfo.id, //产品ID
                productNum: 1, //产品数量
                bottle: _this.data.dataInfo.bottle, //产品规格--多少瓶
                productSpec: _this.data.dataInfo.productSpec, //产品规格--多少毫升
                selectWeight: ~~_this.data.dataInfo.productHeft,
                finallyDistributionTimes: 1, //产品送货几次
                trackFee: 50,
                money: this.data.isInvite?(~~_this.data.dataInfo.inPurchasePrice/100):(_this.data.dataInfo.sellPrice/100)
            }));
            wx.navigateTo({
                url: "/pages/order/order?inPurchase=1"
            })
        } else {
            _this.selectComponent('#login').openLoginModal();
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
    },

    /**
     * 用户点击右上角分享
     */
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function(res) {
        let _this = this;
        const openid = wx.getStorageSync('token');
        if (res.from === 'button') {

        }
        return {
            title: "来自亲友"+this.data.userInfo.name+"的推荐"+(this.data.dataInfo.productTitle||''),
            path: "/pages/goods-personal/goods-personal?&friendId="+this.data.friendInfo.openId ,
            success: function(res) {
                if (res.errMsg == 'shareAppMessage:ok') {}
            },
            fail: function() {
                if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
            },
            complete: function() {

            }
        }
    }
})
